package com.joelaws.prototype.client.manage;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.SimpleStore;
import com.gwtext.client.data.Store;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.ComboBox;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.layout.FitLayout;
import com.gwtext.client.widgets.layout.FormLayout;
import com.joelaws.prototype.client.data.DatingDetails;
import com.joelaws.prototype.client.data.User;
import com.joelaws.prototype.client.data.DatingDetails.EyeColor;
import com.joelaws.prototype.client.data.DatingDetails.HairColor;
import com.joelaws.prototype.client.rpc.Manage;
import com.joelaws.prototype.client.rpc.ManageAsync;

public class ManageDatingTab extends Panel {

	final private User user;

	public ManageDatingTab(User user) {
		this.user = user;
		setBorder(false);
		setFrame(true);
		setTitle("Manage Dating");
		setLayout(new FitLayout());
		add(getFormPanel());
	}

	public String[][] getAsDoubleArray(Enum<?>[] array) {
		String[][] arr = new String[array.length][1];
		for (int i = 0; i < array.length; i++) {
			arr[i][0] = array[i].name();
		}
		return arr;
	}

	private ComboBox getDateChoices(String type, Enum<?>[] choices) {

		final Store store = new SimpleStore(new String[] { "type" }, getAsDoubleArray(choices));
		store.load();

		final ComboBox cb = new ComboBox();
		cb.setForceSelection(true);
		cb.setMinChars(1);
		cb.setFieldLabel(type);
		cb.setStore(store);
		cb.setDisplayField("type");
		cb.setMode(ComboBox.LOCAL);
		cb.setTriggerAction(ComboBox.ALL);
		cb.setEmptyText("Select " + type);
		cb.setLoadingText("Searching...");
		cb.setTypeAhead(true);
		cb.setSelectOnFocus(true);
		cb.setWidth(200);

		cb.setHideTrigger(false);
		return cb;
	}

	private Panel getFormPanel() {
		final FormPanel formPanel = new FormPanel();

		formPanel.setAutoScroll(true);
		formPanel.setHeight(400);
		formPanel.setTitle("Profile Details");
		formPanel.setLayout(new FormLayout());

		final TextField height = new TextField("Height", "height");
		formPanel.add(height);

		final ComboBox eyeColor = getDateChoices("EyeColor", DatingDetails.EyeColor.values());
		formPanel.add(eyeColor);

		final ComboBox hairColor = getDateChoices("HairColor", DatingDetails.HairColor.values());
		formPanel.add(hairColor);

		formPanel.add(new Button("Save", new ButtonListenerAdapter() {

			public void onClick(Button button, EventObject e) {
				System.out.println("Saving dating details...");

				EyeColor ecolor = null;
				if (eyeColor.getValue() != null)
					ecolor = EyeColor.valueOf(eyeColor.getValue());

				HairColor hcolor = null;
				if (hairColor.getValue() != null)
					hcolor = HairColor.valueOf(hairColor.getValue());

				DatingDetails datingDetails = new DatingDetails(user.getUserID(), height.getText(), ecolor, hcolor);
				user.setDatingDetails(datingDetails);
				ManageAsync manage = Manage.Util.getInstance();
				System.out.println("Making ajax dating details save...");
				manage.updateUser(user, new AsyncCallback<Object>() {

					public void onFailure(Throwable caught) {
						System.out.println("Failed to update user in ManageDatingTab due to " + caught.getMessage());

					}

					public void onSuccess(Object result) {
						System.out.println("Successfully update users dating details");
						MessageBox.alert("Saved");
					}
				});

			}
		}));

		DatingDetails details = user.getDatingDetails();
		if (details != null) {
			if (details.getHeight() != null) {
				height.setValue(details.getHeight());
			}
			if (details.getEyeColor() != null) {
				eyeColor.setValue(details.getEyeColor().toString());
			}
			if (details.getHairColor() != null) {
				hairColor.setValue(details.getHairColor().toString());
			}
		}

		return formPanel;
	}
}
